package com.stox.client.ui.javafx.chartingy.widget;

import java.util.ArrayList;
import java.util.List;

import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.shape.Line;

import com.stox.client.ui.javafx.chartingy.model.chart.Chart;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;

public class Grid extends Group {
	
	private final Chart<?,?> chart;
	private final List<Line> verticalLines = new ArrayList<>();
	private final List<Line> horizontalLines = new ArrayList<>();
	
	public Grid(Chart<?,?> chart) {
		this.chart = chart;
		UI.style(this, CSS.grid);
		setManaged(false);
		setAutoSizeChildren(false);
	}
		
	public void clearHorizontalLines(){
		getChildren().removeAll(horizontalLines);
		horizontalLines.clear();
	}
	
	public void clearVerticalLines(){
		getChildren().removeAll(verticalLines);
		for(Chart<?,?> chart : chart.getChartingView().getCharts()){
			chart.getGrid().getChildren().removeAll(verticalLines);
		}
		verticalLines.clear();
	}
	
	public void addHorizontalLine(double y){
		Bounds bounds = chart.getChartContentArea().getLayoutBounds();
		Line line = new Line(bounds.getMinX(), y, bounds.getWidth() - 1, y);
		line.setManaged(false);
		horizontalLines.add(line);
		getChildren().add(line);
	}
	
	
	public void addVerticalLine(double x){
		Bounds bounds = chart.getChartContentArea().getLayoutBounds();
		Line line = new Line(x, bounds.getMinY(), x, bounds.getHeight() - 1);
		line.setManaged(false);
		verticalLines.add(line);
		getChildren().add(line);
		
		for(Chart<?,?> chart : chart.getChartingView().getCharts()){
			bounds = chart.getChartContentArea().getLayoutBounds();
			line = new Line(x, bounds.getMinY(), x, bounds.getHeight() - 1);
			line.setManaged(false);
			verticalLines.add(line);
			chart.getGrid().getChildren().add(line);
		}
	}
	
	@Override
	protected void layoutChildren() {
		// No Op
	}

}
